﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Common.Logging;
using SupportLibrary.Keyword;
using SupportLibrary;

namespace CommonKeyword
{
    public class UsefulKeyword
    {
        private Dictionary<string,string> _tconfig;
        private static ILog log = LogManager.GetLogger<UsefulKeyword>();

        public UsefulKeyword(Dictionary<string, string> config)
        { 
            _tconfig = config;
            
        }

        [Automation]
        public void RunCommand(string processName, string methodName,string transactionID)
        {
            
            DateTime starttime = DateTime.Now;
            ProcessClass process = new ProcessClass();
            string argument = methodName + " " + transactionID;
            string result = process.InvokeWinApp(processName, argument);
            DateTime stoptime = DateTime.Now;
            TimeSpan interval = stoptime - starttime;
            log.Debug(result);
            if (result.Contains("TransactionFailed"))
            {
                log.Debug("bankservice:" + methodName + "," + transactionID + "," + interval.Seconds.ToString() + "," + "Failed");
            }
            else
            {
                log.Debug("bankservice:" + methodName + "," + transactionID + "," + interval.Seconds.ToString() + "," + "Successful");
                
            }
            
        }
        [Automation]
        public void InvokeCommand(string csvFile, string command)
        {
            Console.WriteLine("Start:" + DateTime.Now.ToString());
            string arguments = string.Empty;
            string argument = string.Empty;
            string defaultMemory = "-Xmx2M ";
            string defaultDelay = "";

            if (!string.IsNullOrEmpty(_tconfig["defaultMemory"]))
            {
                defaultMemory = "-Xmx" + _tconfig["defaultMemory"] + "M ";
            }

            if (command.Equals("5"))
            {
                if (!string.IsNullOrEmpty(_tconfig["defaultDelay"]))
                   defaultDelay = " " + _tconfig["defaultDelay"];
                 else
                    defaultDelay = " 5";

            }

            arguments = defaultMemory + "VirtualizationCommand \"" + csvFile + "\" " + command + defaultDelay;
            ProcessClass process = new ProcessClass();
            process.InvokeJavaApp(arguments);
            Console.WriteLine("Stop:" + DateTime.Now.ToString());
        }
    }
}
